package com.keqiang.chart.impl.axis.impl.pseudo3d.combined

import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextPaint
import com.keqiang.chart.impl.axis.impl.combined.ChartType
import com.keqiang.chart.impl.axis.render.YAxisRender
import com.keqiang.chart.utils.ObjectPool
import com.keqiang.chart.utils.toRadian
import kotlin.math.cos
import kotlin.math.sin

/**
 * 组合图Y轴绘制器
 *
 * @author Created by wanggaowan on 2023/4/20 09:07
 */
class Combined3DYAxisRender<T : CombinedChart3D>(chart: T, drawLabelByGridLine: Boolean = true) : YAxisRender<T>(chart, drawLabelByGridLine) {

    override fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, labelMaxWidth: Float, gridHeight: Float, xAxisRange: Float) {
        if (chart.mainChart == ChartType.BAR) {
            chart.barChar.yAxisRender.onDraw(canvas, start, top, end, bottom, labelMaxWidth, gridHeight, xAxisRange)
        } else {
            chart.lineChar.yAxisRender.onDraw(canvas, start, top, end, bottom, labelMaxWidth, gridHeight, xAxisRange)
        }
    }

    /**
     * 绘制图表右边轴线图
     */
    fun onDrawEnd(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, labelMaxWidth: Float, gridHeight: Float, xAxisRange: Float) {
        // 绘制轴线
        val yAxis = chart.yAxisEnd ?: return

        val paint = ObjectPool.obtainPaint<Paint>()
        val textPaint = ObjectPool.obtainPaint<TextPaint>(true)
        val tempRectF = ObjectPool.obtainRectF()
        val path = ObjectPool.obtainPath()

        val xAxis = chart.xAxis
        val faceRadian = chart.faceDegrees.toRadian

        // 起始点旋转chart.faceDegrees角度后的坐标
        val xOffset = cos(faceRadian) * xAxis.lineWidth
        val xEnd = end + xOffset
        val y2 = bottom - sin(faceRadian) * xAxis.lineWidth

        val x = xEnd - yAxis.lineWidth / 2f
        setPaint(paint, yAxis.lineColor, yAxis.lineWidth, yAxis.linePathEffect)
        path.moveTo(x, top)
        path.lineTo(x, y2)
        canvas.drawPath(path, paint)
        xAxis.drawRectF.set(xEnd - xOffset, top, end, y2)

        // 绘制y轴文本
        var tempBottom = y2 - yAxis.startPadding
        textPaint.textSize = yAxis.labelTextSize
        textPaint.color = yAxis.labelColor
        setPaint(paint, yAxis.gridLineColor, yAxis.gridLineWidth, yAxis.gridLinePathEffect)

        yAxis.labelList?.let {
            for (index in it.indices) {
                if (labelMaxWidth > 0 && gridHeight > 0) {
                    if (drawLabelByGridLine) {
                        drawLabelByGridLine(canvas, textPaint, index, it, yAxis, gridHeight, xEnd, labelMaxWidth, tempBottom, tempRectF, true)
                    } else {
                        drawLabelByGridCenter(canvas, textPaint, index, it, yAxis, gridHeight, xEnd, labelMaxWidth, tempBottom, tempRectF, true)
                    }
                }

                if (index > 0 && yAxis.drawGridLine) {
                    var startX =
                        if (yAxis.gridUseStartPadding) xEnd - xAxisRange + xAxis.startPadding else xEnd - xAxisRange
                    if (xAxis.drawGridLine && index == it.size - 1) {
                        startX -= xAxis.gridLineWidth / 2
                    }

                    path.reset()
                    path.moveTo(if (yAxis.gridUseEndPadding) xEnd - xAxis.endPadding else xEnd, tempBottom)
                    path.lineTo(startX, tempBottom)
                    canvas.drawPath(path, paint)
                }

                tempBottom -= gridHeight
            }
        }

        ObjectPool.recycleRectF(tempRectF)
        ObjectPool.recyclePaint(paint)
        ObjectPool.recyclePaint(textPaint)
        ObjectPool.recyclePath(path)
    }
}